#!/bin/bash -e

SERVER="localhost"
PORTS="7000"
ROBOTS_NUM=1000
CLIENT_NUM=1
START_INDEX=0

usage() {
    echo "usage: $0 -s server -p ports -r robotNum -c clientNum -i index"
    echo "        server: server ip default [localhost]"
    echo "        ports: server ports like port1,port2,... default [7000]"
    echo "        robotNum: robot number default [1000]"
    echo "        clientNum: start client number default [1]"
    echo "        index: robot name start index default [0]"
}

while getopts "s:p:r:c:i:" option; do
    case "$option" in
        s)
            SERVER=$OPTARG
            ;;
        p)
            PORTS=$OPTARG
            ;;
        r)
            ROBOTS_NUM=$OPTARG
            ;;
        c)
            CLIENT_NUM=$OPTARG
            ;;
        i)
            START_INDEX=$OPTARG
            ;;
        h)
            usage;
            exit 0;
            ;;
        :|\?)
            usage;
            exit 1;
            ;;
    esac
done

cardinality=$[ROBOTS_NUM/CLIENT_NUM]
index=0

signal_handle() {
    ./test-client -H $SERVER -p $PORTS -r $cardinality -l 2 -s $[START_INDEX+index*cardinality] -P $$ > log/stressData_${ROBOTS_NUM}_$[START_INDEX+index*cardinality]_level2.log &
    index=$(($index+1))
    if [ $index -ge $CLIENT_NUM ]; then
        echo "script progress exit"
        exit 0;
    fi
}


trap signal_handle USR1

signal_handle

while true
do
    sleep 2
done
